<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<link href="${apliPath}static/css/default.css" rel="stylesheet"
	type="text/css" />
<link rel="stylesheet" type="text/css"
	href="${apliPath}static/js/themes/metro-solidBlue/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="${apliPath}static/js/themes/icon.css" />
<link type="text/css" rel="stylesheet"
	href="${apliPath}static/css/main.css" />
<script type="text/javascript"
	src="${apliPath}static/js/jquery-1.10.2.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/cryptoJS/core-min.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/cryptoJS/aes.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/cryptoJS/mode-ecb.js"></script>
<script type="text/javascript">
	$(function() {
		loadManageManagerDatagrid();
		//失去焦点触发函数   
		$("#managerCode").next('span').find('input').blur(
				function() {
					if (isManagerCodeAlreadyUsed($('#managerCode').textbox(
							'getValue'))) {
						$("#managerCode").next('span').find('input').focus();
					}
					;
				});
		$($("#managerPassword2").next('span').find('input')).on('input',
				function(e) {
					validatePassword();
				});
	});
	function delManagers(drop) {
		var dropID = new Array();
		var operationLOG = '删除管理员{';
		if ((typeof drop) == 'number') {
			dropID.push(parseInt(drop));
			operationLOG = operationLOG
					+ $('#manageManager').datagrid('getRows')[rowIndex].code
					+ ',';
		}
		if ((typeof drop) == 'string') {
			$.each($('#manageManager').datagrid('getChecked'), function(index,
					row) {
				dropID.push(row.id);
				operationLOG = operationLOG + row.code + ',';
			});
		}
		operationLOG = operationLOG.substring(0, operationLOG.length - 1);
		operationLOG = operationLOG + '}';
		$.messager.confirm('确认', '您确认想要删除管理员吗？', function(r) {
			if (r) {
				var transData = new Object();
				transData.dropID = dropID.toString();
				transData.operationLOG = operationLOG;
				$.post("${apliPath}manager/drop", transData, function(data) {
					$.messager.show({
						timeout : 2200,
						title : "提示框",
						msg : "已经删除"
					});
					$("#manageManager").datagrid("reload");
				});
			} else {
				alert("幸亏你没有删除");
			}
		});
	}
	function updateAuthDone() {
		var roleRightsNode = $('#roleRightsTree').combotree('tree').tree(
				'getChecked', [ 'checked', 'indeterminate' ]);
		var roleRights = new Array();
		var rolesStr = "";
		$.each(roleRightsNode, function(index, node) {
			roleRights.push(node.id);
			rolesStr = rolesStr + node.text + ',';
		});
		rolesStr = rolesStr.substring(0, rolesStr.length - 1);
		var code = $('#manageManager').datagrid('getSelected').code;
		var id = $('#manageManager').datagrid('getSelected').id;
		console.info(roleRights);
		$.post("${apliPath}role/saveAuthByManager", {
			rolesID : roleRights.toString(),
			id : id,
			operationLOG : '更新管理员[' + code + ']的角色为：{' + rolesStr + '}'
		}, function(data) {
			if (!data) {
				$('#win_editManagerAuth').window('close');
				$("#manageManager").datagrid("reload");
				$.messager.show({
					timeout : 2200,
					title : "修改角色",
					msg : "已经修改"
				});
			}
		});

	}
	//不支持直接传输过来Row数据
	//开启管理员窗口编辑对话
	function dig_modifyManagerOpen(index) {
		rowIndex = index;
		$('#hidden_managerType').val('edit');
		$('#managerName').textbox('setValue',
				$('#manageManager').datagrid('getRows')[index].name);
		$('#managerCode').textbox('initValue',
				$('#manageManager').datagrid('getRows')[index].code);
		$('#managerPhone').textbox('setValue',
				$('#manageManager').datagrid('getRows')[index].phone);
		$('#managerPassword').textbox('setValue', 'setPassword');
		$('#managerPassword2').textbox('setValue', 'setPassword');
		$('#dig_modifyManager').dialog('open');
	}
	//修改管理员信息改变
	function dig_modifyManagerDone() {
		var id = null;
		if ($('#manageManager').datagrid('getRows')[rowIndex] != null) {
			id = $('#manageManager').datagrid('getRows')[rowIndex].id;
		}
		;
		var name = $('#managerName').textbox('getValue');
		var code = $('#managerCode').textbox('getValue');
		var phone = $('#managerPhone').textbox('getValue');
		var password = Encrypt(
				document.getElementById("managerPassword").value,
				"proudToLoveAdios");
		var transData = new Object();
		transData.id = id;
		transData.name = name;
		transData.code = code;
		transData.phone = phone;
		var manageManagerUrl = '';
		if ($('#hidden_managerType').val() == 'edit') {
			if ($('#managerPassword').textbox('getValue') != 'setPassword') {
				transData.password = password;
			}
			manageManagerUrl = '${apliPath}manager/modifyManager';
			transData.operationLOG = '更改管理员[' + code + ']：{' + '名字：' + name
					+ '；标识：' + code + '；手机：' + phone + '}';
		} else {
			//如果要是添加的话  必须输入密码
			transData.password = password;
			manageManagerUrl = '${apliPath}manager/insert';
			transData.operationLOG = '添加管理员[' + code + ']：{' + '名字：' + name
					+ '；标识：' + code + '；手机：' + phone + '}';
		}

		$.post(manageManagerUrl, transData, function(data) {
			$('#dig_modifyManager').dialog('close');
			$("#manageManager").datagrid("reload");
			$.messager.show({
				timeout : 2200,
				title : "提示框",
				msg : "操作成功",
			});
		});
	}
</script>
<input type="hidden" id="hidden_managerType" />
<table id="manageManager">
</table>

<div id="toolBar" style="float: right">
	<table>
		<tr>
			<td><a href="javascript:void(0)" class="easyui-linkbutton"
				data-options="iconCls:'icon-cancel',plain:true"> 删除</a></td>
			<td><span style="font-size: 12px">查询管理</span>&nbsp;<input
				type="text" value="想多删？多点几次试试。。" /></td>
		</tr>
	</table>
</div>
<div id="win_editManagerAuth" class="easyui-window" title="权限选择"
	data-options="closed:true,maximizable:false,minimizable:false,draggable:false,resizable:false,collapsible:false"
	style="width: 400; height: 333;top: 120">
	<div style="margin: 15 0 0 20">
		<select id="roleRightsTree" class="easyui-combotree"
			style="width: 350" data-options="required:true"></select>
	</div>
	<div
		style="text-align: right; vertical-align: bottom; margin-top: 200; padding: 10px 20 10;">
		<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'"
			href="javascript:void(0)" onclick="javascript:updateAuthDone()"
			style="width: 80px">提交</a> <a class="easyui-linkbutton"
			data-options="iconCls:'icon-cancel'" href="javascript:void(0)"
			onclick="javascript:$('#win_editManagerAuth').window('close')"
			style="width: 80px">取消</a>
	</div>
</div>
<div id="modifyManagerDIV">
	<div id="dig_modifyManager" class="easyui-dialog" title="管理员信息修改"
		style="width: 420px; height: 330px; padding: 10px;top: 120"
		data-options="
		        closed : true,
				toolbar: '#dlg-toolbar',
				buttons: '#dlg-buttons',
				onClose: function(){
				    $('#dig_modifyManager #managerName').textbox('clear');
				    $('#dig_modifyManager #managerCode').textbox('clear');
				    $('#dig_modifyManager #managerPhone').textbox('clear');
				    document.getElementById('passwordAlert').innerHTML='';
				    document.getElementById('managerCodeAlert').innerHTML='';
				    $('#managerPassword').textbox('clear');
		            $('#managerPassword2').textbox('clear');
				}
			">
		<table width="400" border="0" cellpadding="0" cellspacing="0"
			style="font-size:12px; margin:2px;">
			<tbody>
				<tr>
					<td width="108" height="38" align="right">管理员名称：</td>
					<td><input class="easyui-textbox" id="managerName"
						style="width: 60%; height: 30; padding: 12"
						data-options="prompt:'managerName',iconCls:'icon-man',iconWidth:38"></td>
				</tr>
				<tr>
					<td width="108" height="38" align="right">管理登录名：</td>
					<td><input class="easyui-textbox" id="managerCode"
						style="width: 60%; height: 30; padding: 12"
						data-options="prompt:'mangerCode',iconCls:'icon-man',iconWidth:38,onChange:function(){isManagerCodeAlreadyUsed(this.value)}">&nbsp;<span
						id="managerCodeAlert"></span></td>
				</tr>

				<tr>
					<td width="108" height="38" align="right">管理员手机：</td>
					<td><input class="easyui-textbox" id="managerPhone"
						style="width: 60%; height: 30; padding: 12"
						data-options="prompt:'managerPhone',iconCls:'icon-man',iconWidth:38"></td>
				</tr>
				<tr>
					<td width="108" height="38" align="right">管理员密码：</td>
					<td><input class="easyui-textbox" id="managerPassword"
						style="width: 60%; height: 30; padding: 12" type="password"
						data-options="prompt:'managerPassword',iconCls:'icon-lock',iconWidth:38"></td>
				</tr>
				<tr>
					<td width="108" height="38" align="right">再次输入密码：</td>
					<td><input class="easyui-textbox" id="managerPassword2"
						style="width: 60%; height: 30; padding: 12" type="password"
						data-options="prompt:'managerPassword',iconCls:'icon-lock',iconWidth:38"><span
						id="passwordAlert"></span></td>
				</tr>
			</tbody>
		</table>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton" id="doneButton"
			onclick="javascript:dig_modifyManagerDone()">存起来</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			onclick="javascript:$('#dig_modifyManager').dialog('close')">先取消</a>
	</div>
</div>


<script type="text/javascript">
	//每次点击修改操作的时候赋值
	var rowIndex = 0;
	//验证管理员Code是否引用
	function isManagerCodeAlreadyUsed(codeValue) {
		var returnFlag = true;
		if (codeValue.length == 0 && codeValue == "") {
			document.getElementById("managerCodeAlert").innerHTML = "<font color='red'>&nbsp;不能为空</font>";
			return returnFlag;
		} else {
			var id = 0;
			if ($('#hidden_managerType').val() == 'edit') {
				id = $('#manageManager').datagrid('getRows')[rowIndex].id;
			}
			$
					.ajax({
						type : "post",
						url : "${apliPath}manager/isManagerCodeAlreadyUsed",
						async : false,
						data : {
							"code" : codeValue,
							"id" : id
						},
						success : function(data) {
							var managerCodeAlert = document
									.getElementById("managerCodeAlert");
							//创建一个元素insertbefore  +
							//var span = document.createElement("span");
							//$(span).css("color","red").css("font-size","5px");
							//managerCodeInput.parentNode.insertBefore(span,managerCodeInput.nextSibling.nextSibling);
							//创建一个元素insertbefore 
							if (data == true) {
								$(managerCodeAlert).css('lang', 0);
								managerCodeAlert.innerHTML = "<font color='red'>&nbsp;重复， 不可用</font>";
								$('#doneButton').linkbutton('disable');
								returnFlag = true;
							} else {
								$(managerCodeAlert).css('lang', 1);
								$('#doneButton').linkbutton('enable');
								managerCodeAlert.innerHTML = "<font color='green'>&nbsp;可用</font>";
								returnFlag = false;
							}
						}
					});
		}
		return returnFlag;
	}
	//加载manageManager表格
	function loadManageManagerDatagrid() {
		var tableOpt = $('#manageManager').datagrid().datagrid('options');
		tableOpt.title = '管理员列表', tableOpt.method = 'post';
		tableOpt.url = '${apliPath}manager/queryManagersByPage';
		tableOpt.pageNumber = 1;
		tableOpt.fit = true;
		tableOpt.checkOnSelect = true;
		tableOpt.singleSelect = false;
		tableOpt.ctrlSelect = true;
		tableOpt.striped = true, tableOpt.loadMsg = '数据正在加载中，请稍等...';
		tableOpt.toolbar = '#toolBar', tableOpt.pagination = true;
		tableOpt.fitColumns = true, tableOpt.pageSize = 9;
		tableOpt.pageList = [ 3, 6, 9, 19, 29 ];
		tableOpt.remoteSort = false;
		tableOpt.iconCls = 'icon-search';
		tableOpt.sortName = 'id';
		tableOpt.sortOrder = 'DESC';
		tableOpt.onLoadSuccess = function(data) {
			$('.delcls').linkbutton({
				text : '删除',
				plain : true,
				iconCls : 'icon-mini-edit'
			});
			$('.modifyManagercls').linkbutton({
				text : '更改信息',
				plain : true,
				iconCls : 'icon-mini-edit'
			});
		};
		//toolbar兼容修改开始 +
		var toolbar = new Array();
		var toolbar0 = new Object();
		toolbar0.text = '分配角色';
		toolbar0.iconCls = 'icon-edit';
		toolbar0.handler = function() {
			if ($('#manageManager').datagrid('getChecked').length != 1) {
				alert('选择一个用户然后进行操作');
			} else {
				$.post("${apliPath}role/queryRoleRightsByManager", {
					"id" : $('#manageManager').datagrid('getSelected').id
				}, function(data) {
					$("#roleRightsTree").combotree({
						data : data,
						multiple : true,
						animate : true,
						checkbox : true
					});
				});
				$('#win_editManagerAuth').window('open');
			}
		};
		var toolbar1 = new Object();
		toolbar1.text = '删除管理员';
		toolbar1.iconCls = 'icon-remove';
		toolbar1.handler = function() {
			delManagers('dropString');
		};
		var toolbar2 = new Object();
		toolbar2.text = '添加管理员';
		toolbar2.iconCls = 'icon-add';
		toolbar2.handler = function() {
			$('#hidden_managerType').val('add');
			$('#dig_modifyManager').dialog('setTitle', '添加管理员');
			$('#dig_modifyManager').dialog('open');
		};
		toolbar.push(toolbar0);
		toolbar.push(toolbar1);
		toolbar.push(toolbar2);
		tableOpt.toolbar = toolbar;
		//toolbar兼容修改结束 -
		//columns兼容修改开始 +
		var columns = new Array();
		var columnArray = new Array();
		var column0 = new Object();
		column0.field = 'id';
		column0.checkbox = true;
		var column1 = new Object();
		column1.title = "管理员名称";
		column1.field = "name";
		column1.width = 70;
		column1.sortable = true;
		var column2 = new Object();
		column2.title = "salt";
		column2.field = "salt";
		column2.width = 80;
		var column3 = new Object();
		column3.title = "登录标识";
		column3.field = "code";
		column3.width = 70;
		column3.sortable = true;
		var column4 = new Object();
		column4.title = "手机";
		column4.field = "phone";
		column4.width = 70;
		var column5 = new Object();
		column5.title = "角色", column5.field = "roles", column5.width = 70,
				column5.formatter = function(value, row, index) {
					var roles = "";
					for (var i = 0; i < row.roles.length; i++) {
						roles = roles + row.roles[i].name + "  ";
					}
					return roles;
				};
		var column6 = new Object();
				column6.title = "编辑操作",
				column6.field = "opt",
				column6.width = 80,
				column6.formatter = function(value, row, index) {
					return '<a class="delcls" onclick="delManagers('
							+ row.id
							+ ')" href="javascript:void(0)"></a><a class="modifyManagercls" onclick="dig_modifyManagerOpen('
							+ index + ')" href="javascript:void(0)"></a>';
				};
		columnArray.push(column0);
		columnArray.push(column1);
		columnArray.push(column2);
		columnArray.push(column3);
		columnArray.push(column4);
		columnArray.push(column5);
		columnArray.push(column6);
		columns.push(columnArray);
		tableOpt.columns = columns;
		//columns兼容修改结束 +
		$('#manageManager').datagrid(tableOpt);
		var p = $('#manageManager').datagrid('getPager');
		if (p) {
			$(p).pagination(
					{
						links : 5,
						beforePageText : '第',
						afterPageText : '页 共 {pages}页',
						displayMsg : '显示 {from}到{to} ,共 {total}条记录',
						layout : [ 'list', 'first', 'prev', 'links', 'next',
								'last', 'manual' ]
					});
		}
	}
	//验证两个密码是否登录一致
	function validatePassword() {
		var pwd1 = $('#managerPassword').textbox('getText');
		var pwd2 = $('#managerPassword2').textbox('getText');
		if (pwd1 == pwd2) {
			document.getElementById("passwordAlert").innerHTML = "<font color='green'>&nbsp;两次密码相同</font>";
			$('#doneButton').linkbutton('enable');
		} else {
			document.getElementById("passwordAlert").innerHTML = "<font color='red'>&nbsp;两次密码不相同</font>";
			$('#doneButton').linkbutton('disable');
		}
	}
	function Encrypt(word, key) {
		var secreKey = CryptoJS.enc.Utf8.parse(key);
		var srcs = CryptoJS.enc.Utf8.parse(word);
		var encrypted = CryptoJS.AES.encrypt(srcs, secreKey, {
			mode : CryptoJS.mode.ECB,
			padding : CryptoJS.pad.Pkcs7
		});
		return encrypted.toString();
	}
	function Decrypt(word, key) {
		var secreKey = CryptoJS.enc.Utf8.parse(key);
		var decrypt = CryptoJS.AES.decrypt(word, secreKey, {
			mode : CryptoJS.mode.ECB,
			padding : CryptoJS.pad.Pkcs7
		});
		return CryptoJS.enc.Utf8.stringify(decrypt).toString();
	}
</script>



